package com.daluzy.chmanager.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.daluzy.chmanager.R;
import com.daluzy.chmanager.a_interface.RecyclerViewItemOnClickListener;
import com.daluzy.chmanager.bean.School;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SelectSchoolAdapter extends RecyclerView.Adapter<SelectSchoolAdapter.MyViewHolder> {


    private Context context;
    private List<School> myData = new ArrayList<>();
    private RecyclerViewItemOnClickListener<School> itemOnClickListener;

    private int selectedId = -1;

    public SelectSchoolAdapter(Context context, List<School> myData) {
        this.context = context;
        this.myData = myData;
    }

    public void setItemOnClickListener(RecyclerViewItemOnClickListener<School> itemOnClickListener) {
        this.itemOnClickListener = itemOnClickListener;
    }

    public School getSelectedSchool() {
        if (selectedId != -1 && myData.size() > selectedId) {
            return myData.get(selectedId);
        } else return null;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.select_school_item_daluzy, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        School school = myData.get(position);
        if (school.isSelect()) {
            holder.ivSelect.setImageResource(R.drawable.radio_selected);
        } else {
            holder.ivSelect.setImageResource(R.drawable.radio_unselecte);
        }
        holder.tvSchoolName.setText(school.getSchool_name());
        holder.tvSchoolAddress.setText(school.getSchool_address());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                itemOnClickListener.itemOnClick(school, position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return myData.size();
    }

    public void updateItem(int position) {
        if (selectedId != -1) {
            School school = myData.get(selectedId);
            school.setSelect(false);
            notifyItemChanged(selectedId, school);
        }
        School school = myData.get(position);
        school.setSelect(true);
        notifyItemChanged(position, school);

        selectedId = position;
    }


    class MyViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_select)
        ImageView ivSelect;
        @BindView(R.id.tv_school_name)
        TextView tvSchoolName;
        @BindView(R.id.tv_school_address)
        TextView tvSchoolAddress;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
